<?php  	
	//session_start();
	require_once("includes/header.php"); 
	require_once("include_dao.php");	
	
	if (isset($_GET['id']))
		$id = $_GET['id'];
	else
		$id = 1;
	
	if (isset($_GET['action']))
		$action = $_GET['action'];
	else
		$action = "empty";
	
	switch($action){
	
		case "add":
			if(isset($_SESSION['ShoppingCart'][$id]))
				$_SESSION['ShoppingCart'][$id]["quantity"]++;
			else
				$_SESSION['ShoppingCart'][$id]["quantity"]=1;
		break;
		
		case "remove":
			if(isset($_SESSION['ShoppingCart'][$id]))
			{
				$_SESSION['ShoppingCart'][$id]["quantity"]--;
				if($_SESSION['ShoppingCart'][$id]["quantity"]==0)
					unset($_SESSION['ShoppingCart'][$id]);
			}
			
		break;
		case "removeProd":
			if(isset($_SESSION['ShoppingCart'][$id])){
				unset($_SESSION['ShoppingCart'][$id]);
			}
		break;
	}
				
?>

<div id="mlist">
    <div id="main">    
     	       
        <div class="rightside">
            <div align="center">
                <h1> Venta de Peliculas </h1>
            </div>
            <?php	
                if(isset($_GET["err"])){
                    if ($_GET["err"] != ''){
                        echo("<marquee>");
                        // Mensajes de error 		
                            $msn =  msn_error($_GET["err"]);
                            echo($msn);
                        // Fin de Mensajes de Error			
                        echo("</marquee>");
                    }
                }
            ?>
            
	        <section class="main">     
	            <form class="form-2" action="paypalconnection.php">        
            	<br />                
                <br /> <br />                         	
                <ul class="list">            
                    <li>
                        <?php	
                            //Inicializamos el contador de productos seleccionados.
                            $totalcoste = 0;							
                            $xTotal = 0;
                            if(isset($_SESSION['ShoppingCart']))
                            {							
                                echo "<table border=0 cellspacing=5 cellpadding=3 width='95%' align='center'>";
                                
                                
                                echo "<tr>";
                                    echo "<td align='center'>Producto</td>";
                                    echo "<td align='center'>Cantidad</td>";
                                    echo "<td align='center' width='20%' >Acción</td>";
                                    echo "<td colspan=2 align=right>Total</td>";
                                echo "</tr>";
                                
                                echo "<tr>";
                                    echo "<td colspan=5><hr></td>";
                                echo "</tr>";
                                
                                
                                foreach ($_SESSION['ShoppingCart'] as $ShoppingCart){
                                    
                                    //acortamos el nombre del producto a 40 caracteres
                                    $producto = substr($ShoppingCart["name"],0,40);
                                    $precio = $ShoppingCart["priceTotal"];
									$cantidad = $ShoppingCart["quantity"];
                                    
                                    //Coste por artículo según la cantidad elegida
                                    $coste = $precio * $cantidad;
                                    
                                    //Coste total del ShoppingCart
                                    $totalcoste +=  $coste;
                                                                                
                                    echo "<tr>";
                                    echo "<td align='left'> $producto </td>";
                                    echo "<td align='center'>$cantidad</td>";
                                    
                                    echo "<td align='center'>";
                                    echo "<a href='ShoppingCart.php?id=". $ShoppingCart["id"] ."&action=removeProd'>
                                            <img src='img/eliminar.png' style='padding:0 0px 0 5px;' alt='Reducir cantidad' />
                                         </a>";
									echo "<a href='ShoppingCart.php?id=". $ShoppingCart["id"] ."&action=add'>
                                                <img src='img/aumentar.png' style='padding:0 0px 0 5px;' alt='Aumentar cantidad' />	
                                              </a>";
									echo "<a href='ShoppingCart.php?id=". $ShoppingCart["id"] ."&action=remove'>
                                                <img src='img/restar.png' style='padding:0 0px 0 5px;' alt='Reducir cantidad' />
                                              </a>";
								 
                                    echo "</td>";
                                    
                                    echo "<td align='right'> = </td>";
                                    echo "<td align='right' style='margin-left:10px'> $ $coste";
                                    echo "</tr>";
                                }
                                echo "<tr><td colspan='5'><hr></td></tr>";
                                echo "<tr>";
                                echo "<td align='right' colspan='4'><b><br>Total = </b></td>";
                                echo "<td align='right'><b><br>$ $totalcoste </b>  </td>";
                                echo "<input name='total' type='hidden' value='$totalcoste'>";
                                echo "</tr>";
                                
                                if($totalcoste != 0){
                                    //BOTON COMPRAR
                                    echo "<tr>";
                                    
                                    
                                    echo "<td align='right' colspan='5'>
                                            <p class='submit'>
                                                <button title='Comprar' id'create-user'>
                                                    <i class='icon-ok icon-large'></i>
                                                </button>
                                            </p>
                                          
                                         </td>";
                                    echo "</tr>";
                                }
                                
                                echo "</table>";
                                                            
                            }
                            else{
                                echo "El carrito está vacío";
                            }                                                    
                        ?>
                    </li>
                </ul>            
         		</form>                
        	</section>
        </div>
           
        <div class="clear"></div>
        
    </div>
</div>     
<?php	
		require_once("includes/footer.php");	
?>